package com.yueqian.controller;

import com.github.pagehelper.PageInfo;
import com.yueqian.domain.Orders;
import com.yueqian.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("orders")
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;

    //注入服务层对象
//    @RequestMapping("findAll.do")
//    public ModelAndView findAll() throws Exception {
//        //调用服务层方法
//        List<Orders> ordersList = ordersService.findAll();
//
//        ModelAndView mav = new ModelAndView();
//        mav.setViewName("orders-list");
//        //添加数据到域对象
//        mav.addObject("ordersList",ordersList);
//        return mav;
//    }
    //   orders/findAll.do?page=&pageSize=
    @RequestMapping("findAll.do")
//    @RequestMapping(value = {"",""})
    public ModelAndView findAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer pageSize) throws Exception {
        //调用服务层方法 //ordersList查出来的数据就已经是分页查找的部分数据
        List<Orders> ordersList = ordersService.findAll(page, pageSize);
        //给页面响应5个分页相关的数据: 当前页pageNum, 每页显示条目pageSize,总记录数total,总页数pages,查出的具体order:
        PageInfo<Orders> pageInfo = new PageInfo<Orders>(ordersList);
        //封装具体的数据,将查出的数据设置到分页插件中的PageInfo类中,在该类中解析了数据,并将5个数据做了拆分
        ModelAndView mav = new ModelAndView();
        mav.setViewName("orders-pages-list");
        //添加数据到域对象
        mav.addObject("pageInfo", pageInfo);
        return mav;
    }

    @RequestMapping("findById.do")
    public ModelAndView findById(String id) throws Exception {
        //调用服务层
        Orders orders = ordersService.findById(id);
        ModelAndView mav = new ModelAndView();
        //封装数据
        mav.addObject("orders", orders);
        mav.setViewName("orders-show");
        return mav;
    }
}
